package com.czy.booktest02.demos.api.v1;

import com.czy.booktest02.demos.common.dtos.ResponseResult;
import com.czy.booktest02.demos.pojo.dto.*;
import com.czy.booktest02.demos.pojo.po.Book;
import com.czy.booktest02.demos.pojo.po.Books;
import com.czy.booktest02.demos.service.BookService;
import com.czy.booktest02.demos.utils.Code;
import com.czy.booktest02.demos.utils.R;
import com.czy.booktest02.demos.utils.TimeUtil;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.UUID;

@RestController
@RequestMapping("/book/maintain")
public class BookInfoController {

    @Autowired
    BookService bookService;
    @PostMapping("/addOrUpdateBook")
    public ResponseResult addOrUpdateBook( @RequestBody BookDto bookDto){
        return bookService.addOrUpdateBook(bookDto);
    }

    @GetMapping("/categoryAndId")
    public ResponseResult categoryAndId(){
        return bookService.categoryAndId();
    }

    @GetMapping("/publisherAndId")
    public ResponseResult publisherAndId(){
        return bookService.publisherAndId();
    }

    //是否推荐
    @PostMapping("/recommend")
    public ResponseResult recommend(@RequestBody BookRASDto bookRASDto){
        return bookService.recommend(bookRASDto);
    }

    //是否下架
    @PostMapping("/status")
    public ResponseResult status(@RequestBody BookRASDto bookRASDto){
        return bookService.status(bookRASDto);
    }

    /**
     * 删除图书
     */
    @PostMapping("/deleteBook")
    public ResponseResult deleteBook( @RequestBody DeleteBookDto deleteBookDto){
        return bookService.deleteBook(deleteBookDto.getBookName());
    }

    /**
     * 图书类别管理
     * 增加或者删除
     */
    @PostMapping("/manageCategory")
    public ResponseResult managerCategory( @RequestBody BookCOrPDto bookCOrPDto){
        return bookService.manageCategory(bookCOrPDto.getCategory());
    }

    /**
     * 修改种类
     */
    @PostMapping("/updateCategory")
    public ResponseResult updateCategory(@Valid @RequestBody CategoryUpdateDto categoryUpdateDto){
        return bookService.updateCategory(categoryUpdateDto);
    }

    /**
     * 出版社管理
     * 增加或删除
     */
    @PostMapping("/managePublisher")
    public ResponseResult managerPublisher(@RequestBody BookCOrPDto bookCOrPDto){
        return bookService.managePublisher(bookCOrPDto.getPublisher());
    }

    /**
     * 修改出版社
     */
    @PostMapping("/updatePublisher")
    public ResponseResult updatePublisher(@RequestBody PublisherUpdateDto publisherUpdateDto){
        return bookService.updatePublisher(publisherUpdateDto);
    }

    /**
     * 展示图书
     */
    @GetMapping("/showBook")
    public ResponseResult showBook(){
        return bookService.showBook();
    }

    /**
     * 根据id获取书本信息
     * @param id
     * @return
     */
    @GetMapping("/getBookId")
    public R getOneBook(@PathVariable("id") Integer id){
        Books result = bookService.getOneBook(id);
        if (!Strings.isNotEmpty(result.getTitle())){
            return new R(Code.WORK_ERR,"根据id获取书本信息失败！");
        }
        return new R(Code.WORK_OK,"获取书本信息成功",result);
    }
}
